/*!
 * @file PropertyActuator.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/PropertyActuator>

#include <osgODE/Notify>
#include <osgODE/RigidBody>
#include <osgODE/World>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
PropertyActuator::PropertyActuator(Mode mode):
    m_mode          ( mode ),
    m_dirty         ( true )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
PropertyActuator::PropertyActuator(const PropertyActuator& other, const osg::CopyOp& copyop):
    Actuator                ( other, copyop ),
    m_mode                  ( other.m_mode ),
    m_property              ( other.m_property ),
    m_reference_object      ( other.m_reference_object ),
    m_reference_property    ( other.m_reference_property ),
    m_object                ( other.m_object ),
    m_dirty                 ( other.m_dirty )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
PropertyActuator::~PropertyActuator(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
PropertyActuator::operator()( osgODE::RigidBody* body, Game* game )
{
    PS_ASSERT1( body != NULL ) ;
    PS_ASSERT1( body->getWorld() != NULL ) ;


    (void) game ;


    if( isActive() ) {


        if( m_dirty ) {

            if( m_reference_object != "" ) {


                m_object = body->getWorld()->getObjectByName( m_reference_object ) ;

                m_reference_object = "" ;

                if( ! m_object.valid() ) {


                    PS_FATAL("ooGame::PropertyActuator(%p): cannot find object \"%s\"", this, m_reference_object.c_str()) ;


                }

                m_dirty = false ;
            }

        } // m_dirty



        actuate( body, game ) ;


    } // isActive()
}
/* ....................................................................... */
/* ======================================================================= */
